﻿using LiveCharts.Wpf;
using LiveCharts;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using HandyControl.Tools.Extension;

namespace BaseUi.ViewModels
{
    public class BarChartViewModel: BindableBase
    {
        private List<string> labels;
        public List<string> Labels
        {
            get { return labels; }
            set { labels = value; }
        }
        //本地保存路径
        public static readonly string DailyStatisticsPath = System.IO.Path.Combine(LocalFile.DataDir, "outputStat");
        //文件锁
        static object lockObject = new object();
        private DateTime StartTime_ = DateTime.Now.Date;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime
        {
            get => StartTime_;
            set
            {
                StartTime_ = value;
            }
        }
        public SeriesCollection SeriesCollection { get; set; }
        public ChartValues<int> Valuesok = new ChartValues<int> { 0, 0, 0, 0, 0 };
        public ChartValues<int> Valuesng = new ChartValues<int> { 0, 0, 0, 0, 0 };
        public BarChartViewModel()
        {
            Labels = new List<string>()
            {
                "总产量",
                "1号测试位",
                "2号测试位",
                "3号测试位",
                "4号测试位",
            };
            SeriesCollection = new SeriesCollection
            {
                new ColumnSeries
                {
                    Title = "ok",
                    Values = Valuesok,
                    Width=10,MaxColumnWidth = 20,DataLabels=true,
        }
                ,
                new ColumnSeries
                {
                    Title = "ng",
                    Values = Valuesng,
                    Width=10,MaxColumnWidth = 20,DataLabels=true,
                },
            };
        }
        //public void updata()
        //{
        //    try
        //    {
        //        Valuesok[1] = ;
        //        Valuesok[2] = ;
        //        Valuesok[3] = ;
        //        Valuesok[4] = ;
        //        Valuesok[0] = ;

        //        Valuesng[1] = ;
        //        Valuesng[2] = ;
        //        Valuesng[3] = ;
        //        Valuesng[4] = ;
        //        Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
        //    }
        //    catch (Exception ee)
        //    {
        //        Logs.Write(ee);
        //        Valuesok[0] = -1;
        //        Valuesok[1] = -1;
        //        Valuesok[2] = -1;
        //        Valuesok[3] = -1;
        //        Valuesok[4] = -1;

        //        Valuesng[0] = -1;
        //        Valuesng[1] = -1;
        //        Valuesng[2] = -1;
        //        Valuesng[3] = -1;
        //        Valuesng[4] = -1;
        //    }
        //}
        public void AddOk(int nu,int num = 1)
        {
            if (num < 1)
                return;

            //重新计算
            if (StartTime.Date != DateTime.Now.Date)
                Reset();

            Valuesok[nu] += num;
            Valuesok[0] = Valuesok[1] + Valuesok[2] + Valuesok[3] + Valuesok[4];
            SaveData();
        }

        public void AddNg(int nu, int num = 1)
        {
            if (num < 1)
                return;

            //重新计算
            if (StartTime.Date != DateTime.Now.Date)
                Reset();

            Valuesng[nu] += num;
            Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
            SaveData();
        }

        public void UpdataData()
        {
            try
            {
                if (!File.Exists(DailyStatisticsPath))
                    return;

                lock (lockObject)
                {
                    var jo = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(DailyStatisticsPath, Encoding.UTF8));

                    if (jo != null)
                    {
                        Valuesok[1] = jo.Value<int>("ok1");
                        Valuesng[1] = jo.Value<int>("ng1");
                        Valuesok[2] = jo.Value<int>("ok2");
                        Valuesng[2] = jo.Value<int>("ng2");
                        Valuesok[3] = jo.Value<int>("ok3");
                        Valuesng[3] = jo.Value<int>("ng3");
                        Valuesok[4] = jo.Value<int>("ok4");
                        Valuesng[4] = jo.Value<int>("ng4");
                        Valuesok[0] = Valuesok[1] + Valuesok[2] + Valuesok[3] + Valuesok[4];
                        Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
                        StartTime = jo.Value<DateTime>("dt");
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        public void SaveData()
        {
            try
            {
                var data = new { ok1 = Valuesok[1], ng1 = Valuesng[1], ok2 = Valuesok[2], ng2 = Valuesng[2], ok3 = Valuesok[3], ng3 = Valuesng[3], ok4 = Valuesok[4], ng4 = Valuesng[4], dt = StartTime };
                lock (lockObject)
                    File.WriteAllText(DailyStatisticsPath, JsonConvert.SerializeObject(data), Encoding.UTF8);
            }
            catch (Exception ee)
            {
                ee.ToString();
            }
        }

        public void Reset()
        {
            string path = Path.Combine(LocalFile.LogDir, "Timenumber", $"{(StartTime.ToString("yyyyMMdd"))}{".txt"}");
            if (!File.Exists(path))
            {
                var data = new { ok1 = Valuesok[1], ng1 = Valuesng[1], ok2 = Valuesok[2], ng2 = Valuesng[2], ok3 = Valuesok[3], ng3 = Valuesng[3], ok4 = Valuesok[4], ng4 = Valuesng[4], dt = StartTime };
                File.WriteAllText(path, JsonConvert.SerializeObject(data), Encoding.UTF8);
            }
            Valuesok =  new ChartValues<int> { 0, 0, 0, 0, 0 };
            Valuesng = new ChartValues<int> { 0, 0, 0, 0, 0 };
            StartTime = DateTime.Now.Date;
        }
    }
}
